package project.active;

import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;
import models.util.UtilInitLang;

import project.dao.ProjectDAO;
import project.model.PrProject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import approve.action.MainFrame;
import approve.action.R;

public class Projectlist extends Activity {
	Bundle language;
	Button bt;
	ListView lvpro;
	Employee emp=new Employee();
	String lang = "EN",database="admin";
	UtilInitLang utilang=new UtilInitLang();
//	AutoCompleteTextView autoSearchpro;	
//	ArrayAdapter<PrProject> adapterSearchpro ;
	L_list_itiempro adprolist;
	TextView title;
	
	TextView logout,username;
	
	ProjectDAO prdao=new ProjectDAO();
	@SuppressWarnings("rawtypes")
	List map = null;
	ArrayList<PrProject> lstpro = new ArrayList<PrProject>();
	int count = 0;
	LinearLayout listPages;
	TextView txt_first, txt_last,txt_prev,txt_next;
	ImageView img_next, img_prev;
	int pageSize = 6;
	int pageCount = 0;
	int maxPages = 2;
	int currentPage = 1;
	List<Integer> listCurrentPages = new ArrayList<Integer>();
	
	public UtilInitLang getUtilang() {
		return utilang;
	}

	public void setUtilang(UtilInitLang utilang) {
		this.utilang = utilang;
	}

	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.projectlist);
		language = getIntent().getExtras().getBundle(getLang());
		//bt=(Button)findViewById(R.id.btBMenuProListToMain);
		
		Intent in = this.getIntent();
		emp = (Employee)in.getSerializableExtra("em");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		txt_prev=(TextView) findViewById(R.id.txt_prev);txt_prev.setText("<<");
		txt_next=(TextView) findViewById(R.id.txt_next);txt_next.setText(">>");
//		lstpro=loadListViewProject();
		map=getListPro(pageSize, currentPage);
		if (map != null && map.size() > 1) {
			lstpro = (ArrayList<PrProject>) map.get(0);
			count = (Integer) map.get(1);
		}
		lvpro =(ListView)findViewById(R.id.lvLProjectList);
		title=(TextView)findViewById(R.id.l_tv_title_prolist);
		title.setText(language.getString("ac_SUProjectList"));
//		autoSearchpro = (AutoCompleteTextView)findViewById(R.id.autocomplete_searchpro);
		
		if(lstpro.size()>0){
//			lvpro.setAdapter(new ArrayAdapter<PrProject>(this, android.R.layout.simple_list_item_1, lstpro));
//			adapterSearchpro = new ArrayAdapter<PrProject>(this, R.layout.l_list_itempro,lstpro);
//			autoSearchpro.setAdapter(adapterSearchpro);
			
			adprolist = new L_list_itiempro(this,lstpro,language.getString("ac_lien_STu"),language.getString("ac_lien_SDen"));
			lvpro.setAdapter(adprolist);
		}
		
		//bt.setOnClickListener(goMenu);
		lvpro.setOnItemClickListener(clickPro);		
		
		pageCount = (int) Math.ceil((double) count / pageSize);
		
		if (pageCount > 0) {
			txt_first = (TextView) findViewById(R.id.txt_first);
			if (currentPage != 1) {
				txt_first.setTextColor(Color.BLACK);
				txt_first.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						currentPage = 1;
						onCreate(savedInstanceState);

					}
				});
			}
			txt_last = (TextView) findViewById(R.id.txt_last);
			if (currentPage != pageCount) {
				txt_last.setTextColor(Color.BLACK);
				txt_last.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						currentPage = pageCount;
						onCreate(savedInstanceState);

					}
				});
			}
			
			
			listPages = (LinearLayout) findViewById(R.id.listpages);
			listCurrentPages.clear();
			final int secIndex = (int) Math.ceil((double) currentPage
					/ maxPages);
			//image next prev
			if (currentPage > maxPages) {				
				txt_prev.setTextColor(Color.BLACK);
				txt_prev.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						int secIndexTemp = secIndex - 1;
						int minIndexTemp = (secIndexTemp * maxPages)
								- (maxPages - 1);
						currentPage = minIndexTemp;
						onCreate(savedInstanceState);
					}
				});
//				img_prev.setImageResource(R.drawable.prev);
//				
//				img_prev.setOnClickListener(new View.OnClickListener() {
//					// @Override
//					public void onClick(View v) {
//						int secIndexTemp = secIndex - 1;
//						int minIndexTemp = (secIndexTemp * maxPages)
//								- (maxPages - 1);
//						currentPage = minIndexTemp;
//						onCreate(savedInstanceState);
//					}
//				});
			}

			if (currentPage < pageCount) {
				txt_next.setTextColor(Color.BLACK);
				
				txt_next.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						int secIndexTemp = secIndex + 1;
						int minIndexTemp = (secIndexTemp * maxPages)
								- (maxPages - 1);
						currentPage = minIndexTemp;
						onCreate(savedInstanceState);
					}
				});
//				img_next.setImageResource(R.drawable.next);
//				
//				img_next.setOnClickListener(new View.OnClickListener() {
//					// @Override
//					public void onClick(View v) {
//						int secIndexTemp = secIndex + 1;
//						int minIndexTemp = (secIndexTemp * maxPages)
//								- (maxPages - 1);
//						currentPage = minIndexTemp;
//						onCreate(savedInstanceState);
//					}
//				});
			}
			//list pages
			int maxIndex = secIndex * maxPages;
			int minIndex = maxIndex - (maxPages - 1);

			if (maxIndex <= pageCount) {
				for (int i = minIndex; i <= maxIndex; i++) {
					final int index = i;
					TextView tv = new TextView(this);
					tv.setText(i + "");
					if (currentPage != i) {
						tv.setTextColor(Color.BLACK);
						tv.setOnClickListener(new View.OnClickListener() {
							// @Override
							public void onClick(View v) {
								currentPage = index;
								onCreate(savedInstanceState);
							}
						});
					}
					tv.setPadding(15, 0, 15, 0);

					listPages.addView(tv);
					listCurrentPages.add(i);
				}
			} else {
				for (int i = minIndex; i <= pageCount; i++) {
					final int index = i;
					TextView tv = new TextView(this);
					tv.setText(i + "");
					if (currentPage != i) {
						tv.setTextColor(Color.BLACK);
						tv.setOnClickListener(new View.OnClickListener() {
							// @Override
							public void onClick(View v) {
								currentPage = index;
								onCreate(savedInstanceState);
							}
						});
					}
					tv.setPadding(15, 0, 15, 0);

					listPages.addView(tv);
					listCurrentPages.add(i);
				}
			}
			
		}
		
		
	}
	
	private OnItemClickListener clickPro = new OnItemClickListener() {
//		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			//Goto function
//			PrProject pro=(PrProject)lvpro.getItemAtPosition(position);
//			Intent i=new Intent(Projectlist.this, LProjectdetail.class);
//			i.putExtra("project",pro);
//			i.putExtra("lang", getLang());
//			i.putExtra("database", getDatabase());
//			i.putExtra("em",emp);
//			
//			startActivity(i);
			//End goto function
			
			PrProject pro=(PrProject)lvpro.getItemAtPosition(position);
			Intent i=new Intent(Projectlist.this, LSubprojectlistmenu.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("project",pro);
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("em",emp);	
			
			startActivity(i);
		}
	};
	
//	private OnClickListener goMenu = new OnClickListener() {
//
////		@Override
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			gobackMenu();
//		}
//	};
	
	public void gobackMenu(){//ham nay de chuyen trang do hen
		Intent i= new Intent(this,MainFrame.class);
		i.putExtras(getIntent().getExtras());
		i.putExtra("em",emp);
		i.putExtra("lang", getLang());
		i.putExtra("database", getDatabase());
		
		startActivity(i);
	}
	
	public ArrayList<PrProject> loadListViewProject(){
		ArrayList<PrProject> lst= new ArrayList<PrProject>();
		ProjectDAO prdao=new ProjectDAO();
		lst=prdao.getAllProjectAdmin(getDatabase());
		System.out.println("soluong: "+lst.size());
		return lst;
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Projectlist.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	@SuppressWarnings({ "rawtypes", "static-access" })
	public List getListPro(int limit, int currentPage) {
		return prdao.getprolist(limit, currentPage,getDatabase());
	}
}
